//
//  FirstViewController.m
//  AssistantDriving
//
//  Created by dcr on 2017/8/28.
//  Copyright © 2017年 邓兆太. All rights reserved.
//

#import "FirstViewController.h"
#import "FirstCell.h"
#import "ImageButton.h"
#import "QuestionCollectManager.h"
#import "AnswerViewController.h"
#import "ChapterViewController.h"
#import "EmulateViewController.h"
#import "SecondViewController.h"
#import "ScoreViewController.h"
#import "StatisticsViewController.h"

@interface FirstViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)NSArray *titles;
@property(nonatomic, strong)NSArray *images;
@property(nonatomic, strong)NSArray *analyzeTitles;

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"科目一";
    _titles = [NSArray arrayWithObjects:@"章节练习", @"顺序练习", @"随机练习", @"专项练习", @"仿真模拟考试", nil];
    _images = [NSArray arrayWithObjects:@"7", @"8", @"9", @"10", @"11", nil];
    _analyzeTitles = [NSArray arrayWithObjects:@"我的错题", @"我的收藏", @"我的成绩", @"练习统计", nil];

    UITableView *table = [self tableView];
    [self.view addSubview:table];
    table.dataSource = self;
    table.delegate = self;
    table.scrollEnabled = false;
    //要把这个设置为false，否则在TableView不会置顶显示
    //self.automaticallyAdjustsScrollViewInsets = false;
    [self initWidgets];
}

//界面显示时系统调用
- (void)initWidgets{
    WS(ws);
    CGFloat y = MainScreenHeight - 90;
    for(int i = 0; i < 4; i++){
        ImageButton *btn = [ImageButton buttonWithType:UIButtonTypeSystem];
        btn.backgroundColor = [UIColor clearColor];
        
        [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",i+12]] forState:UIControlStateNormal];
        btn.tag = 2000+i;
        [btn addTarget:self action:@selector(onClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:_analyzeTitles[i] forState:UIControlStateNormal];
        [btn setTitleColor:GeneralColor1 forState:UIControlStateNormal];
        [btn.titleLabel setFont:WeekFont4];
        [btn verticalImageAndTitle:20.0];
        [self.view addSubview:btn];
        CGFloat x = (MainScreenWidth/8.0) + (MainScreenWidth/4.0)*i - 30;
        [btn mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(x);
            make.topMargin.mas_equalTo(y);
            make.size.mas_equalTo(CGSizeMake(60, 60));
        }];
    }
    
    UILabel *line = [UILabel new];
    line.font = WeekFont3;
    line.textAlignment = NSTextAlignmentCenter;
    line.backgroundColor = [UIColor clearColor];
    [line setTextColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]];
    line.numberOfLines = 1;
    line.text = @"......................我的考试分析......................";
    line.userInteractionEnabled = YES;
    
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make){
        make.centerX.equalTo(ws.view.mas_centerX);
        make.topMargin.mas_equalTo(y-50);
        make.size.mas_equalTo(CGSizeMake(MainScreenWidth, 20));
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstCell * cell = [FirstCell cellWithTableView:tableView];
    
    cell.iconImageView.image = [UIImage imageNamed:_images[indexPath.row]];
    cell.titleLabel.text = _titles[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [FirstCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self selectItem:(int)indexPath.row];
}

- (void)selectItem:(int)row{
    switch (row) {
        case 0:
        {
            ChapterViewController *controller = [[ChapterViewController alloc] init];
            controller.title = @"章节练习";
            controller.datas = [DataManager getData:chapterPractice];
            controller.model = chapterPractice;
            UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
            item.title = @"";
            self.navigationItem.backBarButtonItem = item;
            [self showViewController:controller sender:nil];
        }
            break;
        case 1:
        {
            AnswerViewController *controller = [[AnswerViewController alloc]init];
            controller.number = row;
            controller.model = orderPractice;
            [self.navigationController showViewController:controller sender:nil];
        }
            break;
        case 2:
        {
            AnswerViewController *controller = [[AnswerViewController alloc]init];
            controller.number = row;
            controller.model = randomPractice;
            [self.navigationController showViewController:controller sender:nil];
        }
            break;
        case 3:
        {
            ChapterViewController *controller = [[ChapterViewController alloc] init];
            controller.title = @"专项练习";
            controller.datas = [DataManager getData:specialPractice];
            controller.model = specialPractice;
            UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
            item.title = @"";
            self.navigationItem.backBarButtonItem = item;
            [self showViewController:controller sender:nil];
        }
            break;
        default:
        {
            EmulateViewController *controller = [[EmulateViewController alloc] init];
            controller.title = @"仿真模拟考试";
            UIBarButtonItem *item = [[UIBarButtonItem alloc]init];
            item.title = @"";
            self.navigationItem.backBarButtonItem = item;
            [self showViewController:controller sender:nil];
        }
            break;
    }
}

- (void)onClickButton:(ImageButton *)btn{
    if(btn.tag == 2000){
        NSArray *wrongArr = [QuestionCollectManager getWrongQuestion];
        if(wrongArr == nil || wrongArr.count == 0){
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"目前还没有错题!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
                
            }];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:true completion:nil];
            return;
        }
        AnswerViewController *controller = [[AnswerViewController alloc]init];
        controller.model = myWrong;
        controller.title = _analyzeTitles[0];
        [self.navigationController showViewController:controller sender:nil];
    }else if(btn.tag == 2001){
        NSArray *wrongArr = [QuestionCollectManager getCollectQuestion];
        if(wrongArr == nil || wrongArr.count == 0){
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"目前还没有收藏题目!" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
                
            }];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:true completion:nil];
            return;
        }
        AnswerViewController *controller = [[AnswerViewController alloc]init];
        controller.model = myFavorite;
        controller.title = _analyzeTitles[1];
        [self.navigationController showViewController:controller sender:nil];
    }else if(btn.tag == 2002){
        ScoreViewController *controller = [[ScoreViewController alloc]init];
        controller.title = _analyzeTitles[2];
        [self.navigationController showViewController:controller sender:nil];
    }else if(btn.tag == 2003){
        StatisticsViewController *controller = [[StatisticsViewController alloc]init];
        controller.title = _analyzeTitles[3];
        [self.navigationController showViewController:controller sender:nil];
    }
}

@end
